package com.huawei.fastengine.fastview.download.download;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.huawei.fastengine.R;
import com.huawei.fastengine.fastview.Config;
import com.huawei.fastengine.fastview.StartFastAPPEngine;
import com.huawei.fastengine.fastview.download.download.bean.DownloadRequest;
import com.huawei.fastengine.fastview.download.utils.ActivityUtil;
import com.huawei.fastengine.fastview.download.utils.CommonUtils;
import com.huawei.fastengine.fastview.download.utils.PackageUtils;
import com.huawei.fastengine.fastview.download.utils.SettingUtil;
import com.huawei.fastengine.fastview.download.utils.UiHelper;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import com.huawei.fastengine.fastview.startFastappEngine.appmarket.DetailBean;
import com.huawei.fastengine.fastview.startFastappEngine.appmarket.IQueryDetailHandler;
import com.huawei.fastengine.fastview.startFastappEngine.appmarket.IQueryDetailResult;
import com.huawei.fastengine.fastview.startFastappEngine.appmarket.QueryDetailHanlderRegister;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import java.io.File;
import java.text.NumberFormat;
import o.ll;

@SuppressLint({"InflateParams"})
public class HiappDownloadActivity extends Activity implements OnClickListener {
    public static final String ACTION_INSTALL_INFO_FAIL = "action_install_info_fail";
    private static final String TAG = "HiappDownloadActivity";
    private final String PACKAGE_ADDED = "android.intent.action.PACKAGE_ADDED";
    private AlertDialog dlCancelDialog;
    private AlertDialog dlProgressDialog;
    private DownloadAppHandler downloadAppHandler;
    private ProgressBar downloadProgressbar;
    private TextView downloadText;
    private GuideDownloadDlg guideDownloadDlg = new GuideDownloadDlg();
    private Bundle mBundle;
    private String mDetailUrl = "";
    private File mDownLoadFile;
    private InstallInfoReceiver mInstallInfoReceiver;
    private MyInstalledReceiver mInstalledReceiver;
    private DownloadManager mInstance;
    private RpkInfo mRpkinfo;

    class DialogOnKeyListener implements OnKeyListener {
        private DialogOnKeyListener() {
        }

        public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
            if (i != 4 || keyEvent.getAction() != 0) {
                return false;
            }
            if (HiappDownloadActivity.this.dlCancelDialog != null && HiappDownloadActivity.this.dlCancelDialog.isShowing()) {
                FastViewLogUtils.i(HiappDownloadActivity.TAG, "click key back, cancel down dialog dismiss.");
                HiappDownloadActivity.this.dlCancelDialog.dismiss();
                return true;
            } else if (HiappDownloadActivity.this.dlProgressDialog == null || !HiappDownloadActivity.this.dlProgressDialog.isShowing()) {
                FastViewLogUtils.i(HiappDownloadActivity.TAG, "click key back, other case.");
                return false;
            } else {
                FastViewLogUtils.i(HiappDownloadActivity.TAG, "click key back, down dialog dismiss.");
                HiappDownloadActivity.this.showCancelDownloadDialog();
                return true;
            }
        }
    }

    class InstallInfoReceiver extends BroadcastReceiver {
        private InstallInfoReceiver() {
        }

        public void onReceive(Context context, Intent intent) {
            Log.e(HiappDownloadActivity.TAG, "InstallInfoReceiver onReceive ");
            if (intent != null && !CommonUtils.hasParseException(intent)) {
                if ("action_install_info_fail".equals(intent.getAction())) {
                    HiappDownloadActivity.this.onInstallFailed();
                }
            }
        }
    }

    public class MyInstalledReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            if (intent != null && !CommonUtils.hasParseException(intent)) {
                Log.i(HiappDownloadActivity.TAG, "MyInstalledReceiver onReceive");
                if ("android.intent.action.PACKAGE_ADDED".equals(intent.getAction())) {
                    String dataString = intent.getDataString();
                    Log.i(HiappDownloadActivity.TAG, "instal :" + dataString);
                    if ("package:com.huawei.fastapp".equals(dataString)) {
                        HiappDownloadActivity.this.onInstallSuccessed();
                    }
                }
            }
        }
    }

    private void registerReceiver() {
        this.mInstalledReceiver = new MyInstalledReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.PACKAGE_ADDED");
        intentFilter.addDataScheme(HiAnalyticsConstant.BI_KEY_PACKAGE);
        registerReceiver(this.mInstalledReceiver, intentFilter);
    }

    private void showCancelDownloadDialog() {
        Builder builder = new Builder(this);
        builder.setMessage(R.string.fastapp_dl_cancel_download_prompt_ex);
        builder.setPositiveButton(R.string.fastapp_dl_sure_cancel_download, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                HiappDownloadActivity.this.downloadProgressbar.setProgress(0);
                HiappDownloadActivity.this.downloadProgressbar.setMax(0);
                HiappDownloadActivity.this.downloadText.setText("");
                HiappDownloadActivity.this.dlCancelDialog.dismiss();
                HiappDownloadActivity.this.dlProgressDialog.dismiss();
                HiappDownloadActivity.this.mInstance.stopDownload();
                HiappDownloadActivity.this.finish();
            }
        });
        builder.setNegativeButton(R.string.fastapp_exit_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                HiappDownloadActivity.this.dlCancelDialog.dismiss();
            }
        });
        this.dlCancelDialog = builder.create();
        this.dlCancelDialog.setOnKeyListener(new DialogOnKeyListener());
        this.dlCancelDialog.setCanceledOnTouchOutside(false);
        this.dlCancelDialog.show();
    }

    private void showDlProgressDialog() {
        FastViewLogUtils.i("showDlProgressDialog");
        this.dlProgressDialog = new Builder(this).create();
        View inflate = LayoutInflater.from(this).inflate(R.layout.fastapp_progress_dialog, null);
        this.downloadProgressbar = (ProgressBar) inflate.findViewById(R.id.fastapp_third_app_dl_progressbar);
        this.downloadText = (TextView) inflate.findViewById(R.id.fastapp_third_app_dl_progress_text);
        inflate.findViewById(R.id.fastap_cancel_imageview).setOnClickListener(this);
        int dp2px = UiHelper.dp2px(this, 16);
        this.dlProgressDialog.setView(inflate, dp2px, 0, dp2px, 0);
        this.dlProgressDialog.setOnKeyListener(new DialogOnKeyListener());
        this.dlProgressDialog.setCanceledOnTouchOutside(false);
        this.dlProgressDialog.show();
        this.downloadText.setText(NumberFormat.getPercentInstance().format(0.0d));
    }

    private void startDownload(String str, String str2) {
        FastViewLogUtils.d(TAG, RpkInfo.DOWNLOAD_URL + str + " / " + str2);
        DownloadRequest downloadRequest = new DownloadRequest();
        this.mInstance = DownloadManager.getInstance();
        downloadRequest.setUrl(str);
        downloadRequest.setNeedCheck(true);
        if (!TextUtils.isEmpty(str2)) {
            downloadRequest.setHash(str2);
        }
        this.mInstance.downloadAsync(downloadRequest, getApplicationContext(), new 2(this));
    }

    private void unregisterReceiver() {
        if (this.mInstalledReceiver != null) {
            unregisterReceiver(this.mInstalledReceiver);
            this.mInstalledReceiver = null;
        }
    }

    public void onClick(View view) {
        if (view.getId() == R.id.fastap_cancel_imageview && this.dlProgressDialog != null && this.dlProgressDialog.isShowing()) {
            FastViewLogUtils.i(TAG, "click key back, down dialog dismiss.");
            showCancelDownloadDialog();
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Intent intent = getIntent();
        if (intent != null && !CommonUtils.hasParseException(intent)) {
            this.mRpkinfo = new RpkInfo();
            this.mRpkinfo.parse(intent);
            this.mBundle = intent.getBundleExtra("Management");
            IQueryDetailHandler handler = QueryDetailHanlderRegister.getHandler();
            if (handler != null) {
                handler.queryDetail(Config.FASTAPP_PACKAGE_NAME, new IQueryDetailResult() {
                    public void onResult(DetailBean detailBean) {
                        FastViewLogUtils.d(HiappDownloadActivity.TAG, "bean=" + detailBean);
                        if (ActivityUtil.isActivityDestroyed(HiappDownloadActivity.this)) {
                            Log.e(HiappDownloadActivity.TAG, "queryDetail onResult activity is finished or destroyed");
                        } else if (detailBean == null || TextUtils.isEmpty(detailBean.getDownloadUrl())) {
                            Log.e(HiappDownloadActivity.TAG, "bean is null or downloadUrl is null");
                            Toast.makeText(HiappDownloadActivity.this, HiappDownloadActivity.this.getString(SettingUtil.isNetworkAvailable(HiappDownloadActivity.this) ? R.string.fastapp_dl_install_failed : R.string.fastapp_no_network), 0).show();
                            HiappDownloadActivity.this.finish();
                        } else {
                            FastViewLogUtils.d(HiappDownloadActivity.TAG, "bean=" + detailBean);
                            Log.i(HiappDownloadActivity.TAG, "queryDetail OK");
                            HiappDownloadActivity.this.guideDownloadDlg.showGuideDownloadDlg(HiappDownloadActivity.this, new 1(this, detailBean));
                        }
                    }
                });
                registerReceiver();
                this.mInstallInfoReceiver = new InstallInfoReceiver();
                ll.ˏ(this).ˏ(this.mInstallInfoReceiver, new IntentFilter("action_install_info_fail"));
                return;
            }
            Log.e(TAG, "detailHandler is null");
            finish();
        }
    }

    protected void onDestroy() {
        FastViewLogUtils.e(TAG, "onDestroy");
        unregisterReceiver();
        ll.ˏ(this).ˋ(this.mInstallInfoReceiver);
        if (this.mInstance != null) {
            this.mInstance.resetDownload();
        }
        this.guideDownloadDlg.dismissGuideDownloadDlg();
        super.onDestroy();
    }

    public void onInstallFailed() {
        Log.e(TAG, "install failed.");
        Toast.makeText(this, getString(R.string.fastapp_dl_install_failed), 0).show();
        if (this.dlProgressDialog != null) {
            this.dlProgressDialog.dismiss();
        }
        if (this.mInstance != null) {
            DownloadManager downloadManager = this.mInstance;
            DownloadManager.deleteFile(this.mDownLoadFile);
        }
        finish();
    }

    public void onInstallSuccessed() {
        Log.i(TAG, "onInstallSuccessed :");
        if (!ActivityUtil.isActivityDestroyed(this)) {
            if (this.mInstance != null) {
                DownloadManager downloadManager = this.mInstance;
                DownloadManager.deleteFile(this.mDownLoadFile);
            }
            if (this.dlProgressDialog != null) {
                try {
                    this.dlProgressDialog.dismiss();
                } catch (IllegalArgumentException e) {
                    FastViewLogUtils.d(TAG, "dlProgressDialog dismiss IllegalArgumentException");
                }
            }
            if (this.dlCancelDialog != null) {
                try {
                    this.dlCancelDialog.dismiss();
                } catch (IllegalArgumentException e2) {
                    FastViewLogUtils.d(TAG, "dlCancelDialog dismiss IllegalArgumentException");
                }
            }
            if (this.mBundle != null) {
                Log.i(TAG, "openFastappManagementFromDownload");
                StartFastAPPEngine.getInstance().openFastappManagementFromDownload(this.mBundle);
            } else {
                Log.i(TAG, "openFastAppFromDownload");
                StartFastAPPEngine.getInstance().openFastAppFromDownload(this.mRpkinfo);
            }
            finish();
        }
    }

    protected void onResume() {
        super.onResume();
        if (PackageUtils.HWGAMEBOX_PACKAGE_NAME.equals(getApplicationInfo().packageName)) {
            if (PackageUtils.isEngineInstalledforGameBox(this)) {
                onInstallSuccessed();
            }
        } else if (PackageUtils.isEngineInstalled(this)) {
            onInstallSuccessed();
        }
    }
}
